package draftMate;

import java.util.ArrayList;

import draftMate.player.Hitter;
import draftMate.player.Pitcher;
import draftMate.playerNotFoundException;

public class Team {
	private String name;
	private ArrayList<Pitcher> arms;
	private ArrayList<Hitter> bats;
	
	// projected team stats
	
	// pitchers
	
	private int pIP; // innings pitched
	private int pHitsP; // pitcher hits
	private int pWalks;
	private int pER; // earned runs
	private int pWins;
	private double pERA;
	private double pWHIP;
	private int pK;
	private int pSaves;
	
	// hitters
	
	private int pAB; // at bats
	private int pHitsH; // hitter hits
	private int pHR;
	private int pRuns;
	private int pRBI;
	private int pSteals;
	private double pAvg;
	
	public Team(String name) {
		this.name = name;
	}
	
	public void addPitcher(Pitcher pitcher) {
		arms.add(pitcher);
		
		// first add all count stats
		
		pIP += pitcher.getIp();
		pHitsP += pitcher.getHits();
		pWalks += pitcher.getWalks();
		pER += pitcher.getER();
		pWins += pitcher.getWins();
		pK += pitcher.getK();
		pSaves += pitcher.getSaves();
		
		// then re calculate all the avg type stats
		
		pERA = (pER * 9) / (double)pIP;
		pWHIP = (pWalks + pHitsH) / (double)pIP;
	}
	
	public Pitcher removePitcher(String name) throws playerNotFoundException {
		name.toLowerCase();
		
		int i = 0;
		while (i < arms.size() && !arms.get(i).getName().equals(name)) {
			i++;
		}
		
		if (i >= arms.size()) {
			throw new playerNotFoundException();
		}
		
		Pitcher toDrop = arms.remove(i);
		
		// first subtract all count stats
		
		pIP -= toDrop.getIp();
		pHitsP -= toDrop.getHits();
		pWalks -= toDrop.getWalks();
		pER -= toDrop.getER();
		pWins -= toDrop.getWins();
		pK -= toDrop.getK();
		pSaves -= toDrop.getSaves();
		
		// then re calculate all the avg type stats
		
		pERA = (pER * 9) / pIP;
		pWHIP = (pWalks + pHitsH) / pIP;
		
		return toDrop;
	}
	
	public void addHitter(Hitter hitter) {
		bats.add(hitter);
		
		pAB += hitter.getAb();
		pHitsH += hitter.getHits();
		pHR += hitter.getHr();
		pRuns += hitter.getRuns();
		pRBI += hitter.getRbi();
		pSteals += hitter.getSteals();
		
		pAvg = pHitsH / (double)pAB;
	}
	
	public Hitter removeHitter(String name) throws playerNotFoundException {
		name.toLowerCase();
		
		int i = 0;
		while (i < bats.size() && bats.get(i).getName().equals(name)) {
			i++;
		}
		
		if (i >= bats.size()) {
			throw new playerNotFoundException();
		}
		
		Hitter toDrop = bats.remove(i);
		
		pAB -= toDrop.getAb();
		pHitsH -= toDrop.getHits();
		pHR -= toDrop.getHr();
		pRuns -= toDrop.getRuns();
		pRBI -= toDrop.getRbi();
		pSteals -= toDrop.getSteals();
		
		pAvg = pHitsH / (double)pAB;
		
		return toDrop;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getpIP() {
		return pIP;
	}

	public void setpIP(int pIP) {
		this.pIP = pIP;
	}

	public int getpHitsP() {
		return pHitsP;
	}

	public void setpHitsP(int pHitsP) {
		this.pHitsP = pHitsP;
	}

	public int getpWalks() {
		return pWalks;
	}

	public void setpWalks(int pWalks) {
		this.pWalks = pWalks;
	}

	public int getpER() {
		return pER;
	}

	public void setpER(int pER) {
		this.pER = pER;
	}

	public int getpWins() {
		return pWins;
	}

	public void setpWins(int pWins) {
		this.pWins = pWins;
	}

	public double getpERA() {
		return pERA;
	}

	public void setpERA(int pERA) {
		this.pERA = pERA;
	}

	public double getpWHIP() {
		return pWHIP;
	}

	public void setpWHIP(int pWHIP) {
		this.pWHIP = pWHIP;
	}

	public int getpK() {
		return pK;
	}

	public void setpK(int pK) {
		this.pK = pK;
	}

	public int getpSaves() {
		return pSaves;
	}

	public void setpSaves(int pSaves) {
		this.pSaves = pSaves;
	}

	public int getpAB() {
		return pAB;
	}

	public void setpAB(int pAB) {
		this.pAB = pAB;
	}

	public int getpHitsH() {
		return pHitsH;
	}

	public void setpHitsH(int pHitsH) {
		this.pHitsH = pHitsH;
	}

	public int getpHR() {
		return pHR;
	}

	public void setpHR(int pHR) {
		this.pHR = pHR;
	}

	public int getpRuns() {
		return pRuns;
	}

	public void setpRuns(int pRuns) {
		this.pRuns = pRuns;
	}

	public int getpRBI() {
		return pRBI;
	}

	public void setpRBI(int pRBI) {
		this.pRBI = pRBI;
	}

	public int getpSteals() {
		return pSteals;
	}

	public void setpSteals(int pSteals) {
		this.pSteals = pSteals;
	}
	
	public double getAvg() {
		return pAvg;
	}
}
